
#include <iostream>
#include <fstream>

#include "CameraProto.pb.h"
#include "net_agent.h"

#define LOG std::cout << __FILE__ <<" "<< __LINE__<< " " << __FUNCTION__ << " "

void write(std::string name, std::string &msg) {
    std::ofstream ofile;
    ofile.open(name,std::ios::binary);
    ofile << msg;
    ofile.close();
}

std::string read(std::string name) {
    std::string msg;

    std::ifstream t;
    int length;
    t.open(name,std::ios::binary);      // open input file
    t.seekg(0, std::ios::end);    // go to the end
    length = t.tellg();           // report location (this is the length)
    t.seekg(0, std::ios::beg);    // go back to the beginning
    char buffer[length];    // allocate memory for a buffer of appropriate dimension
    t.read(buffer, length);       // read the whole file into the buffer
    t.close();

    msg.assign(buffer,length);

    return msg;
}

static std::string serialize(CameraInfo cameraProto) {
    std::string msg;
    cameraProto.SerializeToString(&msg);
    return msg;
}

static CameraInfo deserialize(std::string &msg) {
    CameraInfo info;
    info.ParseFromString(msg);
    return info;
}

static void WriteCameraList(const std::string file_name) {

    std::string msg;
    CameraInfoList cameraList;
    CameraInfo *camera = cameraList.add_cameralist();

    int camCnt = cameraList.cameralist_size();
    LOG << "write cam cnt: " << camCnt << std::endl;
    cameraList.set_cameracount(camCnt);

    camera->set_id(0);
    camera->set_ip("192.168.1.101");
    camera->set_port(8000);
    camera->set_user("admin");
    camera->set_pwd("cdzs123456");

    cameraList.SerializeToString(&msg);
    LOG << " msg size:" << msg.size() << std::endl;

    std::string fullMsg;
    ProtoResponse response;
    ProtoType type = UPDATE_CAMERA;
    response.set_prototype(UPDATE_CAMERA);
    response.set_result(0);
    response.set_bodylength(msg.size());
    response.set_protobody(msg);
    response.set_resultmsg("msg generated .");

    response.SerializeToString(&fullMsg);
    LOG << " FullMsg size:" << fullMsg.size() << std::endl;
    write(file_name, fullMsg);
//    write(file_name, msg);
}

static void ReadCameraList(std::string path) {


    std::string fullMsg = read(path);
    LOG << " FullMsg size read From FileSys:" << fullMsg.size() << std::endl;
    ProtoResponse response_b;
    response_b.ParseFromString(fullMsg);
    std::string bodyMsg = response_b.protobody();
    LOG << response_b.prototype() << std::endl;
    LOG << response_b.result() << std::endl;
    LOG << response_b.bodylength() << std::endl;

    CameraInfoList cameraList_b;
    cameraList_b.ParseFromString(bodyMsg);
//    cameraList_b.ParseFromString(fullMsg);
    LOG << "cam cnt:" << cameraList_b.cameracount() << std::endl;
    int cameraNum = cameraList_b.cameralist_size();
    for (int i = 0; i < cameraNum; i++) {
        CameraInfo camera = cameraList_b.cameralist(i);
        LOG << camera.ip() << std::endl;
        LOG << camera.port() << std::endl;
        LOG << camera.user() << std::endl;
        LOG << camera.pwd() << std::endl;
    }
}

int main() {

    CameraInfo cameraInfo;
    cameraInfo.set_id(0);
    cameraInfo.set_ip("127.0.0.1");
    cameraInfo.set_port(8000);
    cameraInfo.set_user("admin");
    cameraInfo.set_pwd("cdzs123456");

    std::string msg = serialize(cameraInfo);
    LOG << msg.size() << " : " << msg << std::endl;

    std::string file_name = "./" + cameraInfo.ip() + ".protobin";
    write(file_name, msg);

    std::string readMsg = read(file_name);
    CameraInfo info = deserialize(readMsg);
    LOG << "ip:" << info.ip() << std::endl;
    LOG << "port:" << info.port() << std::endl;

    int port = info.port();

    CompareResponseList respList;
    CompareResponse *resp = respList.add_responselist();
    respList.add_responselist();
    resp->set_channel(55);
    resp->set_ip("188.188.188");


    int size = respList.responselist_size();
    LOG << "size:" << size << std::endl;
    CompareResponse resp_ = respList.responselist(0);
    LOG << "ip:" << resp_.ip() << std::endl;
    LOG << "channel:" << resp_.channel() << std::endl;

    std::string cam101 = "./192.168.1.101.full.protobin";
    WriteCameraList(cam101);
    ReadCameraList(cam101);

    std::cout << "END" << std::endl;
}